<?php
/**
 * 用户管理
 *
 * PHP Version 5.6
 *
 * @author    guanxu <510104561@qq.com>
 * @time      2017/08/11
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Common\ConstantUtil;
use Think\Page;

Class UserController extends CommonController {

    public function index() {
        import("ORG.Think.Page");
        $obj =  M("user")->order("id desc");
        $obj_list = clone $obj;
        $count  = $obj->count();
        $page   = new Page($count,ConstantUtil::ADMIN_PAGE);
        $show   = $page->show();
        $list   = $obj_list->limit($page->firstRow.",".$page->listRows)->select();
        $this->assign([
            "list"  =>  $list ,
            "page" => $show ,
        ]);
        $this->display("user/index");
    }

    public function addView() {
        $this->display("user/add");
    }

    /**
     * 密码修改
     */
    public function passwordEditor() {
        $user_id = session("admin_user")["id"];
        $password = I("password");
        $n_password = I("n_password");
        $user_exist = M("user")->where(["id"=>$user_id,"password"=>md5($password)])->find();
        if(empty($user_exist)) $this->error("旧密码错误！");
        M("user")->where(["id"=>$user_id,"password"=>md5($password)])->save(["password"=>md5($n_password)]);
        $this->success("密码修改成功！");

    }

    public function add() {
        $data["username"]   = I("post.username");
        $data["password"]   = md5(I("post.password"));
        $data["mobile"]     = I("post.mobile");
        $data["rank"]       = I("post.rank");
        $data["name"]       = I("post.name");
        $data["create_time"]= date("Y-m-d H:i:s");
        $data["status"]     =  ConstantUtil::GLOBAL_TRUE;
        try{
            M("user")->add($data);
            $this->success("添加成功!");
        }catch (\PDOException $e) {
            $this->error("添加失败，用户名或者电话重复！");
        }
    }

    public function editorView() {
        $id = I("get.id");
        $list = M("user")->where(["id"=>$id])->find();
        $this->assign(["list"=>$list,"id"=>$id]);
        $this->display("user/editor");
    }


    public function editor() {
        if(I("post.password")) $data["password"] = md5(I("post.password"));
        $data["username"]   = I("post.username");
        $data["mobile"]     = I("post.mobile");
        $data["rank"]       = I("post.rank");
        $data["name"]       = I("post.name");
        $id = I("post.id");
        $result = M("user")->where(["id"=>$id])->save($data);
        if(!empty($result)) {
            $this->success("添加成功!");
        }else {
            $this->error("添加失败，用户名或者电话重复！");

        }


    }
    public function delete() {
        $id = I("get.id");
        M("user")->where(["id"=>$id])->delete();
        $this->success("删除成功!");
    }

    public function update() {
        $id = I("get.id");
        $status = M("user")->where(["id"=>$id])->getField("status");
        $change = $status == ConstantUtil::GLOBAL_TRUE ? ConstantUtil::GLOBAL_FALSE : ConstantUtil::GLOBAL_TRUE;
        M("user")->where(["id"=>$id])->save(["status"=>$change]);
        $this->success("修改成功!");
    }



    public function mobile_code() {

        import('ORG.Think.Page');

        $user_mobile_code =  M("user_mobile_code");
        $user_mobile_code_count = clone $user_mobile_code ;
        $count  = $user_mobile_code_count->count();
        $page   = new Page($count,ConstantUtil::ADMIN_PAGE);
        $show   = $page->show();
        $list   =$user_mobile_code->limit($page->firstRow.','.$page->listRows)->order("create_time desc")->select();
        $this->assign([
            "list"=>$list ,
            "page"=>$show ,

        ]);
        $this->display("user/mobile");
    }

}